package cn.tedu.ivos.application.controller;

import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.base.aop.Log;
import cn.tedu.ivos.base.response.JsonResult;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/v1/application")
public class ApplicationController {

    @Autowired
    private ApplicationService applicationService;

    @PostMapping("/save")
//    @Log
    public JsonResult save(ApplicationSaveParam applicationSaveParam){
        applicationService.saveApplication(applicationSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("/select")
    public  JsonResult selectApplication(ApplicationQuery applicationQuery){
      //模拟抛出异常，80%的概率出错
//      if(Math.random()<0.5){
//          System.out.println(1/0);
//      }
      List<ApplicationVO> applicationVOList = applicationService.selectApplication(applicationQuery);
      return JsonResult.ok(applicationVOList);
    }
    @PostMapping("/cancel/{id}")
    public JsonResult cancel(@PathVariable Long id){
        applicationService.cancel(id);
        return JsonResult.ok();
    }
    //分配用车
    @PostMapping("/distribute/{applicationId}/{vehicleId}")
    public JsonResult distribute(@PathVariable Long applicationId,
                                 @PathVariable Long vehicleId){
        applicationService.distribute(applicationId,vehicleId);
        return JsonResult.ok();
    }
    //还车
    @PostMapping("/back/{applicationId}/{vehicleId}")
    public JsonResult back(@PathVariable Long applicationId,
                                 @PathVariable Long vehicleId){
        applicationService.back(applicationId,vehicleId);
        return JsonResult.ok();
    }
}
